//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "SegmentIntegral.h"
using namespace Cephei::QL::Math::Integrals;
#include <gen/QL/Math/Integrals/Integrator.h>
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Math::Integrals::CSegmentIntegral::CSegmentIntegral (UInt64 intervals) : CIntegrator(CSegmentIntegral::typeid)
{
    try
    {
#ifdef HANDLE
        _phSegmentIntegral = NULL;
#endif
        QuantLib::Size _intervals = (QuantLib::Size)ValueHelper::Convert (intervals);
        _ppSegmentIntegral = new boost::shared_ptr<QuantLib::SegmentIntegral> (new QuantLib::SegmentIntegral ( _intervals ));
        SetIntegrator (boost::dynamic_pointer_cast<QuantLib::Integrator> (*_ppSegmentIntegral));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Math::Integrals::CSegmentIntegral::CSegmentIntegral (boost::shared_ptr<QuantLib::SegmentIntegral>& childNative, Object^ owner) : CIntegrator(CSegmentIntegral::typeid)
{
#ifdef HANDLE
	_phSegmentIntegral = NULL;
#endif
	_ppSegmentIntegral = &childNative;
    _ppIntegrator = new boost::shared_ptr<QuantLib::Integrator> (boost::dynamic_pointer_cast<QuantLib::Integrator> (*_ppSegmentIntegral));
}
Cephei::QL::Math::Integrals::CSegmentIntegral::CSegmentIntegral (QuantLib::SegmentIntegral& childNative, Object^ owner) : CIntegrator(CSegmentIntegral::typeid)
{
#ifdef HANDLE
	_phSegmentIntegral = NULL;
#endif
	_ppSegmentIntegral = new boost::shared_ptr<QuantLib::SegmentIntegral> (&childNative);
    _ppIntegrator = new boost::shared_ptr<QuantLib::Integrator> (boost::dynamic_pointer_cast<QuantLib::Integrator> (*_ppSegmentIntegral));
    _SegmentIntegralOwner = owner;
    _IntegratorOwner = owner;
}

Cephei::QL::Math::Integrals::CSegmentIntegral::CSegmentIntegral (CSegmentIntegral^ copy) : CIntegrator(CSegmentIntegral::typeid)
{
#ifdef HANDLE
	_phSegmentIntegral = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppSegmentIntegral = new boost::shared_ptr<QuantLib::SegmentIntegral> (copy->GetShared());
        _ppIntegrator = new boost::shared_ptr<QuantLib::Integrator> (boost::dynamic_pointer_cast<QuantLib::Integrator> (*_ppSegmentIntegral));
    }
}
Cephei::QL::Math::Integrals::CSegmentIntegral::CSegmentIntegral (System::Type^ t) : CIntegrator(CSegmentIntegral::typeid)
{
#ifdef HANDLE
	_phSegmentIntegral = NULL;
#endif
	if (!t->IsSubclassOf(CSegmentIntegral::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Math::Integrals::CSegmentIntegral::CSegmentIntegral (QuantLib::Handle<QuantLib::SegmentIntegral>& childNative, Object^ owner)  : CIntegrator(CSegmentIntegral::typeid)
{
	_phSegmentIntegral = &childNative;
	_ppSegmentIntegral = &static_cast<boost::shared_ptr<QuantLib::SegmentIntegral>>(childNative.currentLink());
    _ppIntegrator = new boost::shared_ptr<QuantLib::Integrator> (boost::dynamic_pointer_cast<QuantLib::Integrator> (*_ppSegmentIntegral));
    _SegmentIntegralOwner = owner;
}
Cephei::QL::Math::Integrals::CSegmentIntegral::CSegmentIntegral (QuantLib::Handle<QuantLib::SegmentIntegral> childNative)  : CIntegrator(CSegmentIntegral::typeid)
{
	_phSegmentIntegral = &childNative;
	_ppSegmentIntegral = &static_cast<boost::shared_ptr<QuantLib::SegmentIntegral>>(childNative.currentLink());
    _ppIntegrator = new boost::shared_ptr<QuantLib::Integrator> (boost::dynamic_pointer_cast<QuantLib::Integrator> (*_ppSegmentIntegral));
}
#endif
#ifdef STRUCT
Cephei::QL::Math::Integrals::CSegmentIntegral::CSegmentIntegral (QuantLib::SegmentIntegral childNative)  : CIntegrator(CSegmentIntegral::typeid)
{
#ifdef HANDLE
	_phSegmentIntegral = NULL;
#endif
	_ppSegmentIntegral = new boost::shared_ptr<QuantLib::SegmentIntegral> (new QuantLib::SegmentIntegral (childNative));
    _ppIntegrator = new boost::shared_ptr<QuantLib::Integrator> (boost::dynamic_pointer_cast<QuantLib::Integrator> (*_ppSegmentIntegral));
}
#endif

Cephei::QL::Math::Integrals::CSegmentIntegral::~CSegmentIntegral ()
{
    if (_ppSegmentIntegral != NULL)
    {
	    delete _ppSegmentIntegral;
        _ppSegmentIntegral = NULL;
    }
}
Cephei::QL::Math::Integrals::CSegmentIntegral::!CSegmentIntegral ()
{
    if (_ppSegmentIntegral != NULL)
    {
	    delete _ppSegmentIntegral;
    }
}
QuantLib::SegmentIntegral& Cephei::QL::Math::Integrals::CSegmentIntegral::GetReference ()
{
    if (_ppSegmentIntegral == NULL) throw gcnew NativeNullException ();
	return **_ppSegmentIntegral;
}
boost::shared_ptr<QuantLib::SegmentIntegral>& Cephei::QL::Math::Integrals::CSegmentIntegral::GetShared ()
{
    if (_ppSegmentIntegral == NULL) throw gcnew NativeNullException ();
	return *_ppSegmentIntegral;
}
QuantLib::SegmentIntegral* Cephei::QL::Math::Integrals::CSegmentIntegral::GetPointer ()
{
    if (_ppSegmentIntegral == NULL) throw gcnew NativeNullException ();
	return &**_ppSegmentIntegral;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::SegmentIntegral>& Cephei::QL::Math::Integrals::CSegmentIntegral::GetHandle ()
{
	if (_phSegmentIntegral == NULL)
	{
		_phSegmentIntegral = new Handle<QuantLib::SegmentIntegral> (*_ppSegmentIntegral);
	}
	return *_phSegmentIntegral;
}
#endif
bool Cephei::QL::Math::Integrals::CSegmentIntegral::HasNative () 
{
	return (_ppSegmentIntegral != NULL);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Math::Integrals::ISegmentIntegral^ Cephei::QL::Math::Integrals::CSegmentIntegral_Factory::Create (UInt64 intervals)
{
    return gcnew CSegmentIntegral ( intervals);
}
